import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"

enum Api {
  pageDiyForm = "/infra-service/diyForm/page",
  getDiyForm = "/infra-service/diyForm/get",
  saveDiyForm = "/infra-service/diyForm/save",
  updateDiyForm = "/infra-service/diyForm/update",
  deleteDiyForm = "/infra-service/diyForm/delete",
  batchDeleteDiyForm = "/infra-service/diyForm/batchDelete",
  saveGroup = "/infra-service/diyForm/saveGroup",
  updateGroup = "/infra-service/diyForm/updateGroup",
  batchDeleteGroup = "/infra-service/diyForm/batchDeleteGroup",
  deleteGroup = "/infra-service/diyForm/deleteGroup",
  importTable = "/infra-service/diyForm/importTable",
  batchDeleteField = "/infra-service/diyForm/batchDeleteField",
  listField = "/infra-service/diyForm/listField",
}

export const listFormField = (params) => defHttp.get({ url: Api.listField, params })
export const batchDeleteField = (params) => defHttp.delete({ url: `${Api.batchDeleteField}?ids=${params}` })
export const importTable = (params) => defHttp.post({ url: Api.importTable, params })
export const getDiyForm = (params) => defHttp.get({ url: Api.getDiyForm, params })
export const pageDiyForm = (params) => defHttp.get({ url: Api.pageDiyForm, params })

export const saveOrUpdateDiyForm = (params, isUpdate) => {
  const url = isUpdate ? Api.updateDiyForm : Api.saveDiyForm
  return defHttp.post({ url: url, params })
}

export const deleteDiyForm = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteDiyForm, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchDeleteDiyForm = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteDiyForm, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const saveOrUpdateGroup = (params, isUpdate) => {
  const url = isUpdate ? Api.updateGroup : Api.saveGroup
  return defHttp.post({ url: url, params })
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
